
//ajax
function loadXMLDoc()
{
    var xmlhttp;
    if (window.XMLHttpRequest)
    {
        //  IE7+, Firefox, Chrome, Opera, Safari 浏览器执行代码
        xmlhttp=new XMLHttpRequest();
    }
    else
    {
        // IE6, IE5 浏览器执行代码
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange=function()
    {
        if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
            var json = JSON.parse(xmlhttp.responseText);
            console.log(json);
            //数据翻转
            json = json.data.reverse();
            document.querySelector("#list").innerHTML = "";
            for (var i = 0;i<json.length;i++){
                var li = document.createElement("li");
                var spanName = document.createElement("span");
                spanName.innerHTML = json[i].studentName;
                var spanSex = document.createElement("span");
                spanSex.innerHTML = json[i].sex;
                var spanAge = document.createElement("span");
                spanAge.innerHTML = json[i].age;

                li.appendChild(spanName);
                li.appendChild(spanSex);
                li.appendChild(spanAge);

                document.querySelector("#list").appendChild(li);
            }

            //document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
        }
    };
    xmlhttp.open("GET","http://localhost/ajaxApi/public/index.php/index/student/student",true);
    xmlhttp.send();
}
loadXMLDoc();

document.querySelector("#btn").addEventListener("click",function () {
    var n= document.querySelector("#n").value;
    console.log(n);
    var s = document.querySelector("#s").value;
    console.log(s);
    var a = document.querySelector("#a").value;
    console.log(a);

    if (n != ""  && a != ""){
    var xmlhttp;
    if (window.XMLHttpRequest)
    {
        //  IE7+, Firefox, Chrome, Opera, Safari 浏览器执行代码
        xmlhttp=new XMLHttpRequest();
    }
    else
    {
        // IE6, IE5 浏览器执行代码
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange=function()
    {
        if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
            console.log(xmlhttp.responseText);
            //document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
            loadXMLDoc();
        }
    };
    xmlhttp.open("GET","http://localhost/ajaxApi/public/index.php/index/student/addStudent?studentName="+n+"&sex="+s+"&age="+a,true);
    xmlhttp.send();
    }else {
        alert("内容不可以为空！")
    }
});




